package com.ruoyi.service.impl;

import com.ruoyi.entity.VerInspiration;
import com.ruoyi.entity.VerInspirationItem;
import com.ruoyi.mapper.VerInspirationItemMapper;
import com.ruoyi.mapper.VerInspirationMapper;
import com.ruoyi.product.domain.ProductSku;
import com.ruoyi.product.mapper.ProductSkuMapper;
import com.ruoyi.service.VerInspirationItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class VerInspirationItemServiceImpl implements VerInspirationItemService {
    @Autowired
    private VerInspirationItemMapper verInspirationItemMapper;
    @Autowired
    private VerInspirationMapper verInspirationMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Override
    @Transactional
    public void saveInspirationSkuRelation(Long inspirationId, List<Long> skuIds) {
        // 获取当前已关联的SKU ID列表
        List<Long> existingSkuIds = verInspirationItemMapper.selectSkuIdsByInspirationId(inspirationId);

        // 如果新列表为空，则删除所有关联关系
        if (skuIds == null || skuIds.isEmpty()) {
            verInspirationItemMapper.deleteByInspirationId(inspirationId);
            return;
        }

        // 检查是否有重复关联的SKU
        Set<Long> existingSet = new HashSet<>(existingSkuIds);
        List<Long> duplicateSkus = skuIds.stream()
                .filter(existingSet::contains)
                .collect(Collectors.toList());

        // 如果有重复关联的SKU，抛出异常提示用户
        if (!duplicateSkus.isEmpty()) {
            throw new RuntimeException("以下商品已关联: " + duplicateSkus);
        }

        // 计算需要新增的SKU ID（排除已存在的）
        List<Long> toAdd = skuIds.stream()
                .filter(skuId -> !existingSet.contains(skuId))
                .collect(Collectors.toList());

        // 只新增新的关联关系，不删除已存在的
        if (!toAdd.isEmpty()) {
            Date now = new Date();
            List<VerInspirationItem> items = toAdd.stream()
                    .map(skuId -> {
                        VerInspirationItem item = new VerInspirationItem();
                        item.setInspirationId(inspirationId);
                        item.setSkuId(skuId);
                        item.setCreateTime(now);
                        return item;
                    })
                    .collect(Collectors.toList());
            verInspirationItemMapper.batchInsert(items);
        }
    }

    @Override
    public List<ProductSku> getRelatedSkusByInspirationId(Long inspirationId) {
        List<Long> skuIds = verInspirationItemMapper.selectSkuIdsByInspirationId(inspirationId);
        if (skuIds.isEmpty()) {
            return Collections.emptyList();
        }
        return productSkuMapper.selectBatchIds(skuIds);
    }

    @Override
    public List<VerInspiration> getRelatedInspirationsBySkuId(Long skuId) {
        List<Long> inspirationIds = verInspirationItemMapper.selectInspirationIdsBySkuId(skuId);
        if (inspirationIds.isEmpty()) {
            return Collections.emptyList();
        }
        return verInspirationMapper.selectBatchIds(inspirationIds);
    }
}
